package com.zh.web.controller.web.taxi;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.zh.zhbootcommon.config.annotation.VerifyToken;
import com.zh.zhbootcommon.domain.DPage;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.entity.taxi.DCarInfo;
import com.zh.business.service.taxi.IDCarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-13
 */
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/car/v1")
@Api(value = "车辆信息")
@AopLog(type = "车辆信息", stackTraceOnErr = true)
public class DCarInfoController {

    @Autowired
    IDCarInfoService carInfoService;

    @ApiOperation(value = "车辆列表查询接口")
    @PostMapping("/queryCarPage")
    public DResult queryCarPage(@RequestParam int page, @RequestParam int limit, @RequestParam String carText) {
        Page<DCarInfo> carInfoPage = carInfoService.queryCarPage(page, limit, carText);
        DPage<DCarInfo> carInfoDPage = DPage.to(carInfoPage);
        return DResult.of(carInfoDPage);
    }

    @ApiOperation(value = "车辆列表查询单个接口")
    @PostMapping("/queryCarOne")
    public DResult queryCarOne(@RequestParam Integer carId) {
        return DResult.of(carInfoService.queryCarOne(carId));
    }

    @Transactional
    @ApiOperation(value = "车辆列表编辑接口")
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DCarInfo carInfo) {
        boolean flag = false;
        if (carInfo.getCarId() < 0) {
            // 新增
            flag = carInfoService.save(carInfo);
        } else if (carInfo.getCarId() > 0) {
            // 更新
            flag = carInfoService.updateById(carInfo);
        }
        return DResult.of(flag);
    }

    @ApiOperation(value = "车辆删除接口")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer carId) {
        boolean flag = false;
        if (carId > 0) {
            flag = carInfoService.removeCar(carId);
        }
        return DResult.of(flag);
    }

}
